package com.xywy.bloodandroid.act;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xywy.bloodandroid.R;
import com.xywy.bloodandroid.bean.HotDoctorInfo;
import com.xywy.bloodandroid.fragment.ExpertExperienceShareFragement;
import com.xywy.bloodandroid.fragment.PatientCommentFragement;
import com.xywy.bloodandroid.fragment.VisitingScheduleFraement;
import com.xywy.bloodandroid.util.ImageLoader;
import com.xywy.bloodandroid.util.ImageLoader.ImageCallback;

public class DoctorDetailFrameAct extends FragmentActivity implements
		OnClickListener {

	private RelativeLayout left_img;
	private TextView middle_title;

	private TextView comment_text;
	private TextView clinic_hours_text;
	private TextView experience_sharing_text;
	private FragmentManager fm;
	private PatientCommentFragement CommentFragement;
	private VisitingScheduleFraement scheduleFraement;
	private ExpertExperienceShareFragement shareFragement;
	private ImageView img1;
	private ImageView img2;
	private ImageView img3;
	private String name1;
	private String url1;
	private String hospital1;
	private String doctor_appellation1;
	private String recollections1;
	private String doctor_honor1;
	private String doctor_areas1;

	private ImageLoader loader = new ImageLoader();
	private String expertid;
	private String doc_id;
	private TextView consult_tv;
	private TextView free_add_plus_tv;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_doctor_detail);
		initdata();
		initview();
		fm = getSupportFragmentManager();
		initlistener();
		switchFragment(R.id.comment_text);
	}

	private void initdata() {
		Intent intent = getIntent();
		HotDoctorInfo doctor =(HotDoctorInfo) intent.getSerializableExtra("HotDoctorInfo");
		name1 = doctor.getName();
		doc_id = doctor.getDocument_id();
		url1 = doctor.getApp_image();
		hospital1 = doctor.getHospital();
		doctor_appellation1 = doctor.getTitle();
		recollections1 = doctor.getDepart();
		doctor_honor1 = doctor.getTeach();
		doctor_areas1 = doctor.getGoodat();
		expertid = doctor.getExpert_id();

	}

	private void initlistener() {
		left_img.setOnClickListener(this);
		comment_text.setOnClickListener(this);
		clinic_hours_text.setOnClickListener(this);
		experience_sharing_text.setOnClickListener(this);
		
		consult_tv.setOnClickListener(this);
		free_add_plus_tv.setOnClickListener(this);
	}

	private void initview() {

		left_img = (RelativeLayout) findViewById(R.id.left_img);
		middle_title = (TextView) findViewById(R.id.middle_title);
		middle_title.setText(name1);

		consult_tv = (TextView) findViewById(R.id.consult_tv);// 咨询此专家
		free_add_plus_tv = (TextView) findViewById(R.id.free_add_plus_tv);// 免费加号

		TextView doctor_name = (TextView) findViewById(R.id.doctor_name);
		ImageView doctor_img = (ImageView) findViewById(R.id.doctor_img);
		TextView hosptail_name = (TextView) findViewById(R.id.hosptail_name);
		TextView doctor_appellation = (TextView) findViewById(R.id.doctor_appellation);
		TextView recollections = (TextView) findViewById(R.id.recollections);
		TextView doctor_honor = (TextView) findViewById(R.id.doctor_honor1);
		TextView doctor_areas = (TextView) findViewById(R.id.doctor_areas);

		Drawable cachedImage = loader.loadDrawable(url1, doctor_img,
				new ImageCallback() {
					@Override
					public void imageLoaded(Drawable imageDrawable,
							ImageView imageView, String imageUrl) {
						if (imageView != null && imageDrawable != null) {
							imageView.setBackgroundDrawable(imageDrawable);
						}
					}
				});
		if (cachedImage == null) {
			doctor_img.setBackgroundResource(R.drawable.head);
		} else {
			doctor_img.setBackgroundDrawable(cachedImage);
		}

		doctor_name.setText(name1);
		hosptail_name.setText(hospital1);
		doctor_appellation.setText(doctor_appellation1);
		recollections.setText(recollections1);
		doctor_honor.setText(doctor_honor1);
		doctor_areas.setText(doctor_areas1);

		comment_text = (TextView) findViewById(R.id.comment_text);
		clinic_hours_text = (TextView) findViewById(R.id.clinic_hours_text);
		experience_sharing_text = (TextView) findViewById(R.id.experience_sharing_text);

		img1 = (ImageView) findViewById(R.id.img1);
		img2 = (ImageView) findViewById(R.id.img2);
		img3 = (ImageView) findViewById(R.id.img3);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.consult_tv:// 咨询此专家
			break;
		case R.id.free_add_plus_tv:// 免费加号
			break;
		case R.id.left_img:
			finish();
			break;
		case R.id.comment_text:
			switchFragment(R.id.comment_text);
			break;
		case R.id.clinic_hours_text:
			switchFragment(R.id.clinic_hours_text);
			break;
		case R.id.experience_sharing_text:
			switchFragment(R.id.experience_sharing_text);
			break;

		default:
			break;
		}
	}

	/**
	 * ״̬
	 * 
	 * @param id
	 */
	private void switchFragment(int id) {
		FragmentTransaction fragmentTransaction = fm.beginTransaction();
		//
		hideFragments(fragmentTransaction);
		Bundle bundle = new Bundle();
		switch (id) {
		case R.id.comment_text:

			// comment_text.setTextColor(0xff333333);
			// clinic_hours_text .setTextColor(0xff999999);
			// experience_sharing_text .setTextColor(0xff999999);

			img1.setVisibility(View.VISIBLE);
			img2.setVisibility(View.INVISIBLE);
			img3.setVisibility(View.INVISIBLE);

//			bundle.putString("expertid", expertid);
			bundle.putString("expertid", expertid);

			System.out.println("在这获取的expertid...." + expertid);

			if (CommentFragement == null) {
				CommentFragement = new PatientCommentFragement();
				CommentFragement.setArguments(bundle);

				fragmentTransaction.add(R.id.content_frame, CommentFragement);
			} else {
				fragmentTransaction.show(CommentFragement);
			}

			break;
		case R.id.clinic_hours_text:

			img1.setVisibility(View.INVISIBLE);
			img2.setVisibility(View.VISIBLE);
			img3.setVisibility(View.INVISIBLE);
			bundle.putString("expertid", expertid);// 传专家id
			bundle.putString("doc_id", doc_id + "");
			if (scheduleFraement == null) {
				scheduleFraement = new VisitingScheduleFraement();

				scheduleFraement.setArguments(bundle);
				fragmentTransaction.add(R.id.content_frame, scheduleFraement);
			} else {
				fragmentTransaction.show(scheduleFraement);
			}

			break;
		case R.id.experience_sharing_text:

			img1.setVisibility(View.INVISIBLE);
			img2.setVisibility(View.INVISIBLE);
			img3.setVisibility(View.VISIBLE);
			bundle.putString("expertid", expertid);
			if (shareFragement == null) {
				shareFragement = new ExpertExperienceShareFragement();
				shareFragement.setArguments(bundle);
				fragmentTransaction.add(R.id.content_frame, shareFragement);
			} else {
				fragmentTransaction.show(shareFragement);
			}
			break;

		}
		fragmentTransaction.commit();
	}

	/**
	 * 
	 * 
	 * @param transaction
	 * 
	 */
	private void hideFragments(FragmentTransaction ft) {
		if (CommentFragement != null) {
			ft.hide(CommentFragement);
		}

		if (scheduleFraement != null) {
			ft.hide(scheduleFraement);
		}
		if (shareFragement != null) {
			ft.hide(shareFragement);
		}
	}
}
